package org.dx.oho.controller;

import org.dx.oho.entity.Area;
import org.dx.oho.service.AreaService;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by duanx 2020/11/19
 *@description： AreaController
 *@classname: AreaController
 *@author: duanx
 *@date: 2020/11/19 21:08
 **/
@Controller
@RequestMapping(value = "/superadmin")
public class AreaController {

    private final AreaService areaService;
    private Logger logger = LoggerFactory.getLogger(AreaController.class);
    public AreaController(AreaService areaService) {
        this.areaService = areaService;
    }


    /**
     * 查询所有区划信息
     * @return json map
     */
    @NotNull
    @ResponseBody
    @RequestMapping(value = "/listarea",method = RequestMethod.GET)
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> listArea(){
        logger.info("=> 查询所有区划====");
        long startTime = System.currentTimeMillis();
        Map<String,Object> map = new HashMap<>(6);
        List<Area> areaList = new ArrayList<>();
        try {
            areaList = areaService.getAllAreaList();
            map.put("success",true);
            map.put("total",areaList.size());
            logger.info("=> 查询所有区划成功====");
        }catch (Exception e){
            System.out.println(e.toString());
            map.put("success",false);
            map.put("errMsg",e.toString());
            logger.info("=> 查询所有区划失败====");
            logger.error(e.toString());
        }
        map.put("rows",areaList);
        long endTime = System.currentTimeMillis();
        logger.info("=> 查询所有区划结束====");
        logger.info("=> 查询所有区划共耗时：[{}ms]",endTime-startTime);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/listallchildarea")
    public Map<String ,Object> listAllChildArea(Area area){
        logger.info("=> 查询父区划下的所有子区划====");
        Map<String ,Object> modelMap = new HashMap<>(6);
        List<Area> areaList;
        try{
            areaList = areaService.getChileAreaList(area.getAreaId());
            modelMap.put("success",true);
            modelMap.put("total",areaList.size());
            logger.info("=> 查询父区划下的所有子区划成功====");
        }catch (Exception e){
            System.out.println(e.toString());
            modelMap.put("success",false);
            modelMap.put("errMsg",e.toString());
            logger.info("=> 查询父区划下的所有子区划失败====");
            logger.error(e.toString());
        }
        return modelMap;
    }


    /**
     * 查询所有区划信息
     * @return json map
     */
    @NotNull
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true,isolation = Isolation.READ_COMMITTED)
    public Map<String,Object> list(){
        logger.info("=> 查询所有区划====");
        long startTime = System.currentTimeMillis();
        Map<String,Object> map = new HashMap<>(6);
        List<Area> areaList = new ArrayList<>();
        try {
            areaList = areaService.getAllAreaList();
            map.put("success",true);
            map.put("total",areaList.size());
            logger.info("=> 查询所有区划成功====");
        }catch (Exception e){
            System.out.println(e.toString());
            map.put("success",false);
            map.put("errMsg",e.toString());
            logger.info("=> 查询所有区划失败====");
            logger.error(e.toString());
        }
        map.put("rows",areaList);
        long endTime = System.currentTimeMillis();
        logger.info("=> 查询所有区划结束====");
        logger.info("=> 查询所有区划共耗时：[{}ms]",endTime-startTime);
        return map;
    }
    /**
     * 查询所有区划信息
     * @return json map
     */
    @NotNull
    @RequestMapping(value = "/vuehello",method = RequestMethod.GET)
    public ModelAndView vueHello(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("vue/index");
        return modelAndView;
    }

}
